<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="net.sf.json.JSONObject,weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
	BaseBean bb= new BaseBean();
	RecordSet rs = new RecordSet();
	RecordSet rs_mx = new RecordSet();
	RecordSet rs_x = new RecordSet();
	Map<String, String> dataMap = new HashMap<String, String>();
	bb.writeLog("====getLM====");
	String diamert = request.getParameter("diamert");
	String customer = request.getParameter("customer");//客户
	String product = request.getParameter("product");
	diamert = getDiameter(diamert);
	product = getProduct(product);
	String type = request.getParameter("type");
	String ref = request.getParameter("ref");
	ref = getref(ref);
	String prev = request.getParameter("prev");
	//String qzd = customer+diamert+product+"Q";
	String qzd = "";
	String lsh = "";
	String bbh = "";
	String flag = "0";
	String message = "";
	String bm = "";
	if(type.equals("0")){
		//新建
		qzd = customer+diamert+product+ref;
		//查询对应流水和版本是否存在
		String sql_ishavels = "select 1 from uf_lmbm where qzd ='"+qzd+"'";
		bb.writeLog("sql——ishave=="+sql_ishavels);
		rs.execute(sql_ishavels);
		boolean ishave = false;
		if(rs.next()){
			ishave = true;
		}
		if(ishave){
			//之前存在流水
			String sql_max = "select max(lsh) as lsh  from uf_lmbm where qzd = '"+qzd+"'";
			bb.writeLog("sql_max=="+sql_max);
			rs.execute(sql_max);
			String lsh_max = "";
			if(rs.next()){
				lsh_max = Util.null2String(rs.getString("lsh"));
			}
			if(Integer.parseInt(lsh_max)==99){
				message = "error!";
				flag = "1";
			}else{
				lsh = String.valueOf(Integer.parseInt(lsh_max)+1);
				if(lsh.length()==1){
					lsh = "0"+lsh;
				}
				bbh = "0";
				bm = qzd+lsh+bbh;
				message = "SUCCESS!";
			}
			
		}else{
			lsh = "00";
			bbh = "0";
			bm = qzd+lsh+bbh;
			message = "SUCCESS!";
		}		
		
	}else{
		//change
		if(prev.length()<1){
			//prev未选择
			message = "Previous name未选择!";
			flag = "1";
		}else{
			String first = prev.substring(0,7);
			String between = prev.substring(7,9);
			String last = prev.substring(9,10);
			bb.writeLog("first=="+first);
			bb.writeLog("between=="+between);
			bb.writeLog("last=="+last);
			String sql_bbh = "select max(bbh) as bbh from uf_lmbm where qzd='"+first+"' and lsh='"+between+"'";
			String bbh_max = "";
			bb.writeLog("sql_bbh=="+sql_bbh);
			rs.execute(sql_bbh);
			if(rs.next()){
				bbh_max = Util.null2String(rs.getString("bbh"));
			}
			if(Integer.parseInt(bbh_max)==9){
				message="error";
				flag = "1";
			}else{
				String new_bbh = String.valueOf(Integer.parseInt(bbh_max)+1);
				lsh  = between;
				bbh = new_bbh;
				qzd = first;
				bm = qzd+lsh+bbh;
				message = "SUCCESS!";
			}
			
		}
	}
	
	
	
	
	
	dataMap.put("qzd",qzd);
	dataMap.put("bm",bm);
	dataMap.put("bbh",bbh);
	dataMap.put("lsh",lsh);
	dataMap.put("message",message);
	dataMap.put("flag",flag);
	JSONObject json = JSONObject.fromObject(dataMap);
	out.print(json.toString());
%>
<%! 
public String getDiameter(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_dmtdzb where diameter='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}
public String getProduct(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_prodzb where producttype='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}

public String getlm(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select lmname from uf_lasermark where id='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("lmname"));
	}
	return ret;
}
public String getref(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_refdzb where refsemistd='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}
%>

	